#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<iostream>
#include <io.h>

#include<QProcess>
#include<QFileDialog>

using namespace std;

      int i = 0;

      int lenfile = 0;

QString pathname = "";

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->button->setText("");
    QPixmap pixmap("E:\\code_and_works\\Qt6\\openproject\\leftbutton.png");
    QPixmap fitpixmap = pixmap.scaled(30, 30, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    ui->button->setIcon(QIcon(fitpixmap));
    ui->button->setIconSize(QSize(30, 30));
    ui->button->setFlat(true);
    ui->button->setStyleSheet("border: 0px"); //消除边框

    ui->button2->setText("");
    QPixmap pixmap2("E:\\code_and_works\\Qt6\\openproject\\rightbutton.png");
    QPixmap fitpixmap2 = pixmap2.scaled(30, 30, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    ui->button2->setIcon(QIcon(fitpixmap2));
    ui->button2->setIconSize(QSize(30, 30));
    ui->button2->setFlat(true);
    ui->button2->setStyleSheet("border: 0px"); //消除边框


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_button_clicked()
{

    vector<string> files;
    getAllFiles(pathname.toStdString(),files,"png");
    lenfile = files.size();

    if(lenfile>=1){

    if(i<0){
        i = lenfile - 1;
    }
    else if(i > lenfile - 1){
        i = lenfile - 1;
    }

    //lenfile = files.size();
    QString pngname = QString::fromStdString(files[i]);
    pngname.replace("/", "\\"); // 只能识别 "\"
    QPixmap pixmap3(pngname);

    ui->label->setPixmap(pixmap3);


    i = i-1;

    }

}

void MainWindow::getAllFiles(string filepath, std::vector<string> &files, string typefile)
{
        intptr_t hFile = 0; //文件句柄
        struct _finddata_t fileinfo;  //用于存储读取到的文件信息
        string p;
        if ((hFile=_findfirst(p.assign(filepath).append("\\*"+typefile).c_str(),&fileinfo))!=-1)
             do {
                       files.push_back(p.assign(filepath).append("\\\\").append(fileinfo.name));
                   }while(_findnext(hFile,&fileinfo)==0);
             _findclose(hFile);//关闭文件句柄
        cout<< p << endl;
        cout<< files[1] << endl;
}


void MainWindow::on_button2_clicked()
{

    vector<string> files;
    getAllFiles(pathname.toStdString(),files,"png");
    lenfile = files.size();

    if(lenfile>=1){

      if(i<0){
          i = lenfile - 1;
      }
      else if(i > lenfile - 1){
          i = 0;
      }

      //lenfile = files.size();
      QString pngname = QString::fromStdString(files[i]);
      pngname.replace("/", "\\"); // 只能识别 "\"
      QPixmap pixmap3(pngname);

      ui->label->setPixmap(pixmap3);

          i = i+1;
    }
}


void MainWindow::on_pushButton_clicked()
{
    /***
    QProcess process;
    //QString filePath = m_currentProjectPath;
    QString filePath = "e:\\abc";
    filePath.replace("/", "\\"); // 只能识别 "\"
    QString cmd = QString("explorer.exe /select,\"%1\"").arg(filePath);
    qDebug() << cmd;
    process.startDetached(cmd);
    ***/

    //选择文件夹
       QString curPath=QCoreApplication::applicationDirPath(); //获取应用程序的路径
   //  QString curPath=QDir::currentPath();//获取系统当前目录
   //调用打开文件对话框打开一个文件

       QString dlgTitle="选择一个目录"; //对话框标题
       QString selectedDir=QFileDialog::getExistingDirectory(this,dlgTitle,curPath,QFileDialog::ShowDirsOnly);
       if (!selectedDir.isEmpty())
       //ui->plainTextEdit->appendPlainText(selectedDir);
        qDebug() << selectedDir;

       pathname = selectedDir;
}

